
var editIndex = undefined;
function endEditing(){//判断是否所有列符合要求
    if (editIndex == undefined){return true}
    if ($('#dg').datagrid('validateRow', editIndex)){
    $('#dg').datagrid('endEdit', editIndex);
    editIndex = undefined;
    return true;
} else {
    return false;
}
}
function onClickCell(index, field){
    if (editIndex != index){
    if (endEditing()){
    $('#dg').datagrid('selectRow', index)
    .datagrid('beginEdit', index);
    var ed = $('#dg').datagrid('getEditor', {index:index,field:field});
    if (ed){
    ($(ed.target).data('textbox') ? $(ed.target).textbox('textbox') : $(ed.target)).focus();
}
    editIndex = index;
}else {
    setTimeout(function(){
    $('#dg').datagrid('selectRow', editIndex);
},0);
}
}
}

    function append(){
    if (endEditing()){
    $('#dg').datagrid('appendRow',{status:'P'});
    editIndex = $('#dg').datagrid('getRows').length-1;
    $('#dg').datagrid('selectRow', editIndex)
    .datagrid('beginEdit', editIndex);
}
}
    function removeIt(){
    if (editIndex == undefined){return}
    var row = $('#dg').datagrid('getSelected');
    $.messager.confirm('是否删除', '确定删除?', function(r){
    if (r){
    $.get("/admin/adDelStu",{
    "id":row.id,
},function (data){
    if(data.msg==true){
    $('#dg').datagrid('cancelEdit', editIndex)
    .datagrid('deleteRow', editIndex);
    alert("success");
}else {
    alert("删除失败"+data.error);
}
    editIndex = undefined;
    $('#dg').datagrid('reload');
})
}
});

}
    function add(){

}

    function accept(){
    if (endEditing()){
    $('#dg').datagrid('acceptChanges');
    var row =  $('#dg').datagrid('getSelected');
    if(row!=null){
    if(row.id== undefined){
    $.get("/admin/adAddStu",{
    "password":row.password,
    "sno":row.sno,
    "name":row.name,
    "clazzName":row.clazzName,
    "sex":row.sex,
    "age":row.age,
    "address":row.address,
    "phone":row.phone,
    "dormitory" :row.dormitory,
    "native_place":row.native_place,
    /*"row":JSON.stringify(row),*///证明可用
},
    function (msg){
    if(msg.msg==true){
    alert("更新成功");
}else {
    alert("失败"+msg.error);
}
    editIndex = undefined;
    $('#dg').datagrid('reload');
})
}else {
    $.get("/admin/adUpStu",{
    "id":row.id,
    "password":row.password,
    "sno":row.sno,
    "clazz_id":row.clazz_id,
    "name":row.name,
    "clazzName":row.clazzName,
    "sex":row.sex,
    "age":row.age,
    "address":row.address,
    "phone":row.phone,
    "dormitory" :row.dormitory,
    "native_place":row.native_place,
}, function (msg){
    if(msg.msg==true){
    $('#dg').datagrid('acceptChanges');
    alert("更新成功");
}else {
    alert("失败"+msg.error);
}
    $('#dg').datagrid('reload');
})
}
}
}
}



